#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#include <sqlite3.h>

int main(int argc, const char *argv[])
{
	sqlite3 *db;
	sqlite3_stmt *stmt;
	char *err;
	int rc;
	char *sql;
	const char *tail;
	int ncols, i;

	/* The database must exists. */
	rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE, NULL);
	//rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
	//rc = sqlite3_open("test.db", &db);
	if (rc) {
		fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}

	sql = "select * from log;";

	rc = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail);

	if (rc != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
	}

	rc = sqlite3_step(stmt);
	ncols = sqlite3_column_count(stmt);
	while (rc == SQLITE_ROW) {

		for (i = 0; i < ncols; i++) {
			if (sqlite3_column_type(stmt, i) == SQLITE_INTEGER) 
				fprintf(stderr, "%d\t", sqlite3_column_int(stmt, i));
			else
				fprintf(stderr, "'%s'\t", sqlite3_column_text(stmt, i));
		}
		fprintf(stderr, "\n");

		rc = sqlite3_step(stmt);
	}

	sqlite3_finalize(stmt);
	sqlite3_close(db);

	return 0;
}
